package com.xexun.gpstracker.my.activity;

import android.Manifest;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import com.xexun.gpstracker.channel.R;
import com.xexun.gpstracker.base.BaseActivity;
import com.xexun.gpstracker.fix.grant.PermissionsManager;
import com.xexun.gpstracker.fix.grant.PermissionsResultAction;
import com.xexun.gpstracker.listener.PrettyTextChangeListener;
import com.xexun.gpstracker.util.CommUtil;

public class WatchSettingActivity extends BaseActivity {
    @ViewInject(R.id.backBtn)
    private ImageButton backButton;
    @ViewInject(R.id.phoneET)
    private EditText phoneET;
    @ViewInject(R.id.apn)
    private EditText apn;
    @ViewInject(R.id.commitBtn)
    private Button commitButton;

    private final static String SENT_SMS_ACTION = "SENT_SMS_ACTION";
    private SMSSendReceiver smsSendReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_watch_setting);
        x.view().inject(this);

        backButton.setOnClickListener(clickListener);
        commitButton.setOnClickListener(clickListener);

        String phone = getIntent().getStringExtra("phone");
        phoneET.setText(CommUtil.prettyString(phone, null, true, 3, 4));
        phoneET.addTextChangedListener(new PrettyTextChangeListener(3, 4));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (smsSendReceiver != null) {
            unregisterReceiver(smsSendReceiver);
        }
    }

    /**
     * 点击事件
     */
    private OnClickListener clickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.backBtn:
                    WatchSettingActivity.this.finish();
                    break;
                case R.id.commitBtn:
                    saveSetting();
                    break;
            }
        }
    };

    private void saveSetting() {
        final String phone = CommUtil.trimSeprator(phoneET.getText().toString(), null);
        if (CommUtil.isBlank(phone) || CommUtil.isBlank(apn.getText())) {
            CommUtil.showMsgShort(getString(R.string.apn_setting_note));
            return;
        }
        PermissionsManager.getInstance().requestPermissionsIfNecessaryForResult(this, new String[]{Manifest.permission.SEND_SMS}, new PermissionsResultAction() {
            @Override
            public void onGranted() {
                if (smsSendReceiver == null) {
                    smsSendReceiver = new SMSSendReceiver();
                }
                registerReceiver(smsSendReceiver, new IntentFilter(SENT_SMS_ACTION));

                SmsManager sms = SmsManager.getDefault();
                Intent intent = new Intent(SENT_SMS_ACTION);
                PendingIntent sendIntent = PendingIntent.getBroadcast(WatchSettingActivity.this, 0, intent, 0);
                sms.sendTextMessage(phone, null, apn.getText().toString(), sendIntent, null);
            }

            @Override
            public void onDenied(String permission) {
                CommUtil.showMsgShort(getString(R.string.apn_setting_failed));
            }
        });

    }

    class SMSSendReceiver extends BroadcastReceiver {
        //写个接收器
        @Override
        public void onReceive(Context context, Intent intent) {
            int resultCode = getResultCode();
            if (resultCode == Activity.RESULT_OK) {
                CommUtil.showMsgShort(String.format(getString(R.string.success), getString(R.string.apn_setting)));
            } else {
                CommUtil.showMsgShort(String.format(getString(R.string.failed), getString(R.string.apn_setting)));
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionsManager.getInstance().notifyPermissionsChange(permissions, grantResults);
    }
}
